package com.springframework.boot;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient//开启服务注册与发现，新版本默认开启，所以可加可不加，但是euerka必须加类似注解
@EnableFeignClients   //开启openfeign支持
public class SpringcloudImproveUsers9999Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringcloudImproveUsers9999Application.class, args);
    }

    @Bean
    @LoadBalanced//代表ribbon负载均衡的RestTemplate客户端对象
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    /**
     * 新版本中要用HystrixDashboard,则需要在监听的服务中加入如下配置
     * @return
     */
    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }

}
